//
//  AlertView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/18.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "AlertView.h"

@interface AlertView ()

@property (nonatomic, strong) UIView * mainView; // 弹出窗
@property (nonatomic, strong) UILabel * titleLabel; // 标题label
@property (nonatomic, strong) UILabel * messageLabel; // 消息label

@end

@implementation AlertView
#pragma mark 🌷🌷🌷cycle life🌷🌷🌷
- (instancetype)initWithButtonNames:(NSArray *)buttonNames selecIndex:(SelecIndexBlock)selectIndex
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGB_COLOR(0, 0, 0, 0.3);
        
        _selecIndexBlock = selectIndex;
        // 添加视图
        [self addSubview:self.mainView];
        [_mainView addSubview:self.titleLabel];
        [_mainView addSubview:self.messageLabel];
        for (NSInteger i = 0; i < buttonNames.count; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:buttonNames[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor grayColor];
            button.layer.cornerRadius = 10;
            button.layer.masksToBounds = YES;
            button.tag = 10 + i;
            [button addTarget:self action:@selector(respondsToSelect:) forControlEvents:UIControlEventTouchUpInside];
            
            // 添加视图
            [_mainView addSubview:button];
            
            // 自动布局
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self).multipliedBy(0.1);
                make.height.equalTo(self).multipliedBy(0.028);
                if (i == 0) {
                    make.left.offset(SCREEN_WIDTH * 0.01);
                } else {
                    make.right.offset(SCREEN_WIDTH * -0.01);
                }
                make.bottom.offset(SCREEN_WIDTH * -0.01);
            }];
        }
        
        // 自动布局
        [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH * 0.25, buttonNames.count == 0 ? SCREEN_HEIGHT * 0.1 : SCREEN_HEIGHT * 0.14));
            make.center.equalTo(self);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(SCREEN_HEIGHT * 0.016);
            make.centerX.equalTo(_mainView);
        }];
        [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom);
            make.centerX.equalTo(_mainView);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷event response🌷🌷🌷
- (void)respondsToSelect:(UIButton *)sender {
    _selecIndexBlock(sender.tag - 10);
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = RGB_COLOR(0, 0, 0, 0.65);
        _mainView.layer.cornerRadius = 10;
        _mainView.layer.masksToBounds = true;
    }
    return _mainView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"提示";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:25];
    }
    return _titleLabel;
}
- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.text = @"该账号已在其他地方登录\n请重新登录";
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.numberOfLines = 0;
        _messageLabel.textColor = [UIColor whiteColor];
        _messageLabel.font = [UIFont systemFontOfSize:18];
    }
    return _messageLabel;
}

@end
